package ua.kharkiv.khpi.tarasenko.diploma;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class Sequence implements Cloneable {
	private List<Item> sequence = new ArrayList<Item>();

	// There we will store items in our sequence for fast contains method
	private Set<Item> itemSet = new HashSet<Item>();

	@Override
	public String toString() {

		String result = "";
		for (Item item : sequence) {
			result = result + item + " ";
		}
		return result;
	}

	public boolean isEmpty() {
		boolean result = true;
		if (sequence.size() > 0) {
			result = false;
		}

		return result;
	}

	public void addItem(Item item) {
		sequence.add(item);
		itemSet.add(item);
	}

	public void addSequence(Sequence anSequence) {
		sequence.addAll(anSequence.sequence);
		itemSet.addAll(anSequence.sequence);
	}

	public List<Item> getItems() {
		return sequence;
	}

	public Sequence subsequence(int startIndex) {
		Sequence subSeq = new Sequence();
		List<Item> newSeq = Collections.EMPTY_LIST;

		if (!(startIndex >= sequence.size())) {
			newSeq = new ArrayList<Item>(sequence.subList(startIndex,
					sequence.size()));
		}
		subSeq.sequence = newSeq;
		return subSeq;
	}

	// public String[] toStringArray() {
	// String[] array = new String[sequence.size()];
	// for (int i=0; i < sequence.size(); i++) {
	// array[i] = sequence.get(i);
	// }
	// return array;
	// }

	public int indexOf(Item item) {
		int index = sequence.indexOf(item);
		return index;
	}
	
	public boolean contains(Item item) {
		return itemSet.contains(item);
	}
	//
	// public int indexOf(Sequence s) {
	// int index = sequence.indexOf(s.sequence);
	// return index;
	// }
}
